//
//  WSUserInfoController.m
//  CharonTVProject
//
//  Created by karong on 2023/8/4.
//

#import "WSUserInfoController.h"
#import "WSTextFieldAlterController.h"
#import "WSSetUserAvatarController.h"
#import "WSSetUserSexController.h"
#import "WSTextViewAlterController.h"
#import "CTMediator+WSUserCenterAction.h"
#import "WSUserInfoRequest.h"
#import "WSDatePickerView.h"

@interface WSUserInfoController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, WSDatePickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *userImageBtn;
@property (weak, nonatomic) IBOutlet UIButton *userNameBtn;
@property (weak, nonatomic) IBOutlet UIButton *userIdBtn;
@property (weak, nonatomic) IBOutlet UIButton *userSexBtn;
@property (weak, nonatomic) IBOutlet UIButton *userBirthdayBtn;
@property (weak, nonatomic) IBOutlet UIButton *userSignBtn;
@property (weak, nonatomic) IBOutlet UILabel *signPlaceholderLabel;

@property (nonatomic, strong) UIImagePickerController * imagePickerController;
@end

@implementation WSUserInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutView];
}

- (void)layoutView {
    [self.userImageBtn sd_setImageWithURL:[NSURL URLWithString:self.charonTV_user.avatar] forState:(UIControlStateNormal) placeholderImage:WSUserPlaceholderImage];
    [self.userNameBtn setTitle:self.charonTV_user.user_name forState:(UIControlStateNormal)];
    [self.userIdBtn setTitle:WSSF(@"%ld", (long)self.charonTV_user.id) forState:(UIControlStateNormal)];
    [self.userSexBtn setTitle:(@[APPLocalized(@"未知"), APPLocalized(@"男"), APPLocalized(@"女"), APPLocalized(@"未知")])[self.charonTV_user.sex] forState:(UIControlStateNormal)];
    [self.userBirthdayBtn setTitle:self.charonTV_user.birthday forState:(UIControlStateNormal)];
    [self.userSignBtn setTitle:self.charonTV_user.sign forState:(UIControlStateNormal)];
    self.signPlaceholderLabel.hidden = !WSIsNullOrEmpty(self.charonTV_user.sign);
}

- (void)editcharonTV_UserWithField:(NSString *)field stringValue:(NSString *)value success:(charonTV_EmptyBlock)success {
    [WSToast loading];
    [WSUserInfoRequest editcharonTV_UserInfoWithField:field stringValue:value success:^{
        [WSToast dismiss];
        success();
        [self layoutView];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (void)editcharonTV_UserWithField:(NSString *)field integerValue:(NSInteger)value success:(charonTV_EmptyBlock)success {
    [WSToast loading];
    [WSUserInfoRequest editcharonTV_UserInfoWithField:field integerValue:value success:^{
        [WSToast dismiss];
        success();
        [self layoutView];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (IBAction)userImageAction:(id)sender {
    UIViewController *setUserAvatarVC = [[CTMediator sharedInstance]ct_showcharonTV_UserAvatarWithSelectAvatarHandle:^(NSInteger integer) {
        if (integer == 1) {
            //相冊
            self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:self.imagePickerController animated:YES completion:nil];
        }
        else if (integer == 2) {
            //拍照
            self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:self.imagePickerController animated:YES completion:nil];
        }
    }];
    setUserAvatarVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    setUserAvatarVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:setUserAvatarVC animated:YES completion:nil];
}

- (IBAction)userNameAction:(id)sender {
    WeakSelf
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_TFAlterWithTitle:@"" content:self.charonTV_user.user_name placeholderStr:@"修改暱稱" maxWordCount:10 confirm:@"" confirmHandle:^(NSString *string) {
        [weakSelf editcharonTV_UserWithField:@"user_name" stringValue:string success:^{
            weakSelf.charonTV_user.user_name = string;
        }];
    } cancel:@""];
    target.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    target.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:target animated:YES completion:nil];
    
    
}

- (IBAction)userSexAction:(id)sender {
    WeakSelf
    UIViewController *setUserSexVC = [[CTMediator sharedInstance]ct_showcharonTV_UserSexWithSelectSexHandle:^(NSInteger integer) {
        //1男 2女
        [weakSelf editcharonTV_UserWithField:@"sex" integerValue:integer success:^{
            weakSelf.charonTV_user.sex = integer;
        }];
    }];
    setUserSexVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    setUserSexVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:setUserSexVC animated:YES completion:nil];
}

- (IBAction)userBirthdayAction:(id)sender {
    WSDatePickerView* datePickerView = [[WSDatePickerView alloc] init];
    datePickerView.delegate = self;
    [datePickerView setDefaultSelectDay:self.charonTV_user.birthday dateFormat:@"yyy-MM-dd"];
    [datePickerView show];
}

- (IBAction)userSignAction:(id)sender {
    WeakSelf
    WSTextViewAlterController *setUserNameVC = (WSTextViewAlterController *)[[CTMediator sharedInstance]ct_showcharonTV_TVAlterWithTitle:@"個性簽名" content:self.charonTV_user.sign maxWordCount:20 confirm:@"" confirmHandle:^(NSString *string) {
        [weakSelf editcharonTV_UserWithField:@"sign" stringValue:string success:^{
            weakSelf.charonTV_user.sign = string;
            weakSelf.signPlaceholderLabel.hidden = !WSIsNullOrEmpty(weakSelf.charonTV_user.sign);
        }];
    } cancel:@""];
    setUserNameVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    setUserNameVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:setUserNameVC animated:YES completion:nil];
}

#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {
    [picker dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        [self.userImageBtn setImage:image forState:(UIControlStateNormal)];
        // 提交頭像
        WeakSelf
        [WSUserInfoRequest uploadcharonTV_Image:image success:^(NSString *string) {
            [weakSelf editcharonTV_UserWithField:@"avatar" stringValue:string success:^{
                weakSelf.charonTV_user.avatar = string;

            }];
        } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {

        }];
    }];
    
}

- (UIImagePickerController *)imagePickerController {
    if (!_imagePickerController) {
        _imagePickerController = [[UIImagePickerController alloc] init];
        _imagePickerController.delegate = self;
        _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        _imagePickerController.allowsEditing = YES;
    }
    return _imagePickerController;
}

#pragma mark WSDatePickerViewDelegate
- (void)pickerDateView:(BasePickerView *)pickerDateView selectYear:(NSInteger)year selectMonth:(NSInteger)month selectDay:(NSInteger)day {
    NSString * string = WSSF(@"%ld-%ld-%ld", (long)year, (long)month, (long)day);
    [self editcharonTV_UserWithField:@"birthday" stringValue:string success:^{
        self.charonTV_user.birthday = string;
    }];
}

@end
